package com.codeyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeyang.domain.Notice;
import com.codeyang.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/5/27 10:08
 */
@Api(tags = "热搜管理接口")
@RequestMapping("shop/notice")
@RestController
public class NoticeController {


    @Autowired
    private NoticeService noticeService;


    @GetMapping("page")
    @ApiOperation("分页查询热搜")
    public ResponseEntity<Page<Notice>> page(Page<Notice> page, Notice notice) {
        Page<Notice> noticePage = noticeService.page(page, new LambdaQueryWrapper<Notice>()
                .eq(!ObjectUtils.isEmpty(notice.getStatus()), Notice::getStatus, notice.getStatus())
                .like(!StringUtils.isEmpty(notice.getIsTop()), Notice::getIsTop, notice.getIsTop())
                .like(!StringUtils.isEmpty(notice.getContent()), Notice::getContent, notice.getContent())
                .orderByDesc(Notice::getUpdateTime)
        );
        return ResponseEntity.ok(noticePage);
    }


    @PostMapping
    @ApiOperation("新增公告")
    public ResponseEntity<Void> test(@RequestBody Notice notice) {
        noticeService.save(notice);
        return ResponseEntity.ok().build();
    }

    @GetMapping("topNoticeList")
    @ApiOperation("加载公告列表")
    public ResponseEntity<List<Notice>> loadNotices() {
        List<Notice> list = noticeService.findTopNotices();
        return ResponseEntity.ok(list);
    }

}
